package com.ymatou.smsproxy.api.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.smsproxy.api.parameter.SendCallVerifyCodeBean;
import com.ymatou.smsproxy.api.service.SendCallVerifyCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SendSmsCallService;
import com.ymttest.database.mongo.ApplicationConfigBean;
import com.ymttest.database.mongo.ApplicationConfigWapper;
import com.ymttest.database.mongo.ChannelConfigBean;
import com.ymttest.database.mongo.ChannelConfigWapper;
import com.ymttest.database.mongo.PhoneBlackListWapper;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;
/**
*发送语音验证码接口
*
*@author chenjiazhu
*/
@TestSuite
public class Ts_SendCallVerifyCode{
	private static SendCallVerifyCodeBean sendcallverifycodeBean;
	private static SendCallVerifyCodeCall sendcallverifycodeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送语音验证码接口");
	}

	@Before
	public void caseUp() {
		sendcallverifycodeBean=new SendCallVerifyCodeBean();
		sendcallverifycodeCall=new SendCallVerifyCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendcallverifycodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_001() {
		Logger.start(true,"正例-发送短信验证码-中文");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			verifyReturn(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_002() {
		Logger.start(true,"正例-发送短信验证码-英文");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("en");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			verifyReturn(false);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_003() {
		Logger.start(true,"反例-AppId空");
		try {
			//sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(true, sendcallverifycodeCall.getReturnData().contains("匹配的 HTTP 资源"), "验证返回报错信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_004() {
		Logger.start(true,"反例-AppId不存在");
		try {
			sendcallverifycodeBean.setAppId("Test1111");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(ECode.smError, sendcallverifycodeCall.getMessage(), "验证返回报错信息");
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_005() {
		Logger.start(true,"反例-phone空");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(true, sendcallverifycodeCall.getReturnData().contains("匹配的 HTTP 资源"), "验证返回报错信息");
					
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_006() {
		Logger.start(true,"反例-code为null");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(true, sendcallverifycodeCall.getReturnData().contains("匹配的 HTTP 资源"), "验证返回报错信息");
				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_007() {
		Logger.start(true,"反例-language空");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();	
			
			verifyReturn(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_008() {
		Logger.start(true,"反例-language取无效值,目前也能存储进数据库");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zcc");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();
			
			verifyReturn(false);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_009() {
		Logger.start(true,"反例-AppId传空字符串");
		try {
			sendcallverifycodeBean.setAppId("");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(ECode.smError, sendcallverifycodeCall.getMessage(), "验证返回报错信息");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_010() {
		Logger.start(true,"反例-phone传空字符串");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(ECode.smError, sendcallverifycodeCall.getMessage(), "验证返回报错信息");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_011() {
		Logger.start(true,"反例-code为空字符串");
		try {
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("13000000014");
			sendcallverifycodeBean.setCode("");
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			Logger.verifyEquals(ECode.smError, sendcallverifycodeCall.getMessage(), "验证返回报错信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_012() {
		Logger.start(true,"正例：手机号在黑名单中,并且发送时间在有效时间内，中风控，备注：功能未实现");
		try {
			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();
			Date dAfter = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.DAY_OF_MONTH, 1); // 设置为后十天
			dAfter = calendar2.getTime(); // 得到前一天的时间

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			
			new PhoneBlackListWapper().updateCollection("Test", "SendMessage", "10999999999", format.format(dBefore),format.format(dAfter));			

			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("10999999999");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			verifyReturn(true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendCallVerifyCode_013() {
		Logger.start(true,"正例：手机号在黑名单中,但是发送时间不在有效时间内，不中风控");
		try {
			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();
			Date dAfter = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -2); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间

			Calendar calendar2 = Calendar.getInstance(); // 得到日历
			calendar2.setTime(dNow);// 把当前时间赋给日历
			calendar2.add(Calendar.DAY_OF_MONTH, -1); // 设置为后十天
			dAfter = calendar2.getTime(); // 得到前一天的时间

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			
			new PhoneBlackListWapper().updateCollection("Test", "SendMessage", "10999999999", format.format(dBefore),format.format(dAfter));			
			
			sendcallverifycodeBean.setAppId("Test");
			sendcallverifycodeBean.setPhone("10999999999");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(5));
			sendcallverifycodeBean.setLanguage("zh-cn");
			
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();			
			
			verifyReturn(false);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(boolean isInBlackList) throws Exception
	{
		Date now = new Date();
		Thread.sleep(1000);
		
		String appid=sendcallverifycodeBean.getAppId();
		
		String content="IsCall:True , VerifyCode:"+sendcallverifycodeBean.getCode()+" , Language:";
		
		if(sendcallverifycodeBean.getLanguage()!=null)
		{
			content+=sendcallverifycodeBean.getLanguage();
		}
		content+=" , MessageTemplate:";
		String phone=sendcallverifycodeBean.getPhone();
		
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
		
		List<String> phones=new ArrayList<String>();
		phones.add(phone);
		
		SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(appid, content, phones);

		Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
		
		ApplicationConfigBean applicationConfig = new ApplicationConfigWapper().getCollectionByIdAndAppId("Test-SendCallVerifyCode",appid);
		ChannelConfigBean channelConfig = new ChannelConfigWapper().getCollectionById(applicationConfig.getChannelId());
		
		Logger.verifyEquals("SendCallVerifyCode", smsRecord.getApiName(), "验证数据库表SmsRecord中getApiName字段为SendSmsVerifyCode");
		
		if(isInBlackList)
		{
			Logger.verifyIsNull(smsRecord.getChannelId(), "验证中风控时数据库表SmsRecord中ChannelId字段为空");
			Logger.verifyIsNull(smsRecord.getChannelName(), "验证中风控时数据库表SmsRecord中ChannelName字段为空");
		}
		else {
		Logger.verifyEquals(applicationConfig.getChannelId(), smsRecord.getChannelId(), "验证数据库表SmsRecord中ChannelId字段与表ApplicationConfig中ChannelId一致");
		Logger.verifyEquals(channelConfig.getName(), smsRecord.getChannelName(), "验证数据库表SmsRecord中ChannelName字段与表ChannelConfig中Name一致");
		}
		
		Logger.verifyEquals(TestDataManager.getLocalIp(), smsRecord.getClientIp(), "验证数据库表SmsRecord中ClientIp字段与发送端Ip一致");
		String host=EnvSetup.getSystemUrl("new_api_smsproxy").replace("http://", "").replace("/", "");
		Logger.verifyEquals(TestDataManager.getServerIP(host), smsRecord.getServerIp(), "验证数据库表SmsRecord中ServerIp字段与服务端Ip一致");

		Date createdTime=YMTDateUtil.getDateFromMongo(smsRecord.getCreatedTime().toString());
		Date sendedTime=YMTDateUtil.getDateFromMongo(smsRecord.getSendedTime().toString());
	
		Logger.verifyEquals(true, Math.abs(createdTime.getTime()-now.getTime())/(1000*60)<20, "验证数据库表SmsRecord中createdTime字段正确，值为："+smsRecord.getCreatedTime().toString()+",当前时间为"+now.toLocaleString());
		if(isInBlackList)
		{
			Logger.verifyEquals("1900-1-1 0:00:00", sendedTime.toLocaleString(), "验证数据库表SmsRecord中createdTime字段正确，值为："+smsRecord.getSendedTime().toString()+",当前时间为"+now.toLocaleString());
		}
		else {
			Logger.verifyEquals(true, Math.abs(sendedTime.getTime()-now.getTime())/(1000*60)<20, "验证数据库表SmsRecord中sendedTime字段正确，值为："+smsRecord.getSendedTime().toString()+",当前时间为"+now.toLocaleString());
		}
		
		if(isInBlackList)
		{
			Logger.verifyEquals(8, smsRecord.getMessageAction(), "验证数据库表SmsRecord中MessageAction字段值为8:手机号在黑名单中InBlackList");
		}
		else {
			Logger.verifyEquals(7, smsRecord.getMessageAction(), "验证数据库表SmsRecord中MessageAction字段值为7,非生产环境下，正常情况下是 发送消息到服务商失败SendMessageFail = 7");
		}
	}
}